/*

File name		: tableD1.do

Description		: creates table on action-based vs motivation-based persistence for the replication study

Output File		: tableD1.tex
				
*/

set more off, perm
capture restore


*** 001 Analysis/Tabulation ***
use "data/blood_donation/Donation.dta", clear

	drop if id_cb == .
	capture drop _merge
	replace TG1 = 0 if unknownstat == . & contactstat == . 
	rename termindatum entnahmedatum

		// Merge rainfall data
		merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"
		*, keep(match)

		*drop 20 duplicates
		set seed 2
		gen double random = runiform()
		sort id_cb entnahmedatum don random
		duplicates drop entnahmedatum don id_cb, force

		keep if _merge == 3
		rename daily_rainfall_ZH daily_rainfall
		*rename kalendertag daily_rainfall


	/* MAKE SAMPLE COMPARABLE TO EXPERIMENT - - > only use donors who have not 
	  donated for at least 1 year. */
	// No donation in the past year? 
	sort id_cb entnahmedatum

	// Generate duration since last donation
	gen duration_since_donation = .
	foreach i of numlist 14 13 12 11 10 9 8 7 6 5 4 3 2 1{
		
		replace duration_since_donation = entnahmedatum - entnahmedatum[_n - `i'] ///
		if don[_n - `i'] == 1 & id_cb == id_cb[_n - `i']
		
	}
	
	gen duration_since_firstinvitation = .
	sort id_cb entnahmedatum
	foreach i of numlist 1 (1) 20{
		
		replace duration_since_firstinvitation = entnahmedatum - entnahmedatum[_n - `i'] ///
		if id_cb == id_cb[_n - `i']
		
	}
	
	gen no_don_pastyear = 1 if year == 2011 & prev_donation == 0
	replace no_don_pastyear = 1 if year > 2011 & duration_since_donation > 365
	replace no_don_pastyear = 0 if prev_donation > 0 & year == 2011 & no_don_pastyear == .
	replace no_don_pastyear = 0 if year > 2011 & duration_since_donation <= 365 & no_don_pastyear == .
	* & duration_since_donation != .
	*replace no_don_pastyear = 1 if year > 2011 & duration_since_donation == . & ///
	duration_since_firstinvitation > 365 & duration_since_firstinvitation != .

	*gen no_don = 1 if duration_since_donation > 365 & duration_since_donation != .
	*replace no_don = 1 if duration_since_donation == . & ///
	duration_since_firstinvitation > 365 & duration_since_firstinvitation != .

	// Number of events per year per sponsor? 
	egen tag = tag(gr_code year c_num)
	egen distinct = total(tag), by(gr_code year)
	by gr_code, sort: egen max_distinct = max(distinct)
	sort gr_code year entnahmedatum c_num
	by gr_code year: gen distinct_runsum = sum(tag) if tag == 1
	by c_num gr_code year, sort: egen runsum_all = max(distinct_runsum)
	keep if max_distinct == 2

	// Number of invitations per individual?
	egen tag2 = tag(id_cb year c_num)
	egen distinct2 = total(tag2), by(id_cb year)
	by id_cb, sort: egen max_distinct2 = max(distinct2)
	by id_cb year, sort: gen distinct2_runsum = sum(tag2)
	keep if max_distinct2 == 2

	// Generate rainfall variables
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort id_cb entnahmedatum
	gen any_rain_plus1 = any_rain[_n + 1] if id_cb == id_cb[_n + 1]

	// Generate lagged terms
	sort id_cb entnahmedatum
	gen don_plus1 = don[_n + 1] if id_cb == id_cb[_n + 1]
	gen phonecall_plus1 = TG1[_n + 1] if id_cb == id_cb[_n + 1]
	gen rainfall_plus1 = daily_rainfall[_n + 1] if id_cb == id_cb[_n + 1]
	*gen weekly_mean_plus1 = weekly_mean_ZH[_n + 1] if id_cb == id_cb[_n + 1]
	*gen monthly_mean_plus1 = monthly_mean_ZH[_n + 1] if id_cb == id_cb[_n + 1]
	rename TG1 phonecall

	// Binary instruments
	gen strong_rain = 1 if daily_rainfall >= 10
	replace strong_rain = 0 if daily_rainfall < 10
	gen extreme_cold = 1 if daily_high_ZH < 5 & daily_high_ZH != .
	replace extreme_cold = 0 if daily_high_ZH >= 5 & daily_high_ZH != .
	destring daily_low_ZH, replace
	gen frost_day = 1 if daily_low_ZH < 0 & daily_low_ZH != .
	replace frost_day = 0 if daily_low_ZH >= 0 & daily_low_ZH != .
	gen extreme_heat = 1 if daily_high_ZH > 25 & daily_high_ZH != .
	replace extreme_heat = 0 if daily_high_ZH <= 25 & daily_high_ZH != .
	gen adverse_weather = 0 if extreme_cold != .
	replace adverse_weather = 1 if extreme_cold == 1 | extreme_heat == 1 | strong_rain == 1

	foreach j of varlist adverse_weather strong_rain extreme_cold extreme_heat {
		
		sort id_cb entnahmedatum
		gen `j'_plus1 = `j'[_n + 1] if id_cb == id_cb[_n + 1]
		
	}

	gen rain_abovemean = 1 if daily_rainfall > 3
	replace rain_abovemean = 0 if daily_rainfall <= 3
	sort id_cb entnahmedatum
	gen rain_abovemean_plus1 = rain_abovemean[_n + 1] if id_cb == id_cb[_n + 1]

	gen rain_abovemedian = 1 if daily_rainfall >= 2.6
	replace rain_abovemedian = 0 if daily_rainfall < 2.6
	sort id_cb entnahmedatum
	gen rain_abovemedian_plus1 = rain_abovemedian[_n + 1] if id_cb == id_cb[_n + 1]


	// Generate controls
	gen month = month(entnahmedatum)
	gen month_cont = month
	replace month_cont = month + 12 if year == 2012
	replace month_cont = month + 12 if year == 2013
	replace month_cont = month_cont - 3
	tab bltype, gen(bltype_dummy)
	*tab prev_donation, gen(prev_donation_dummy)

	// Quarter indicators to make it comparable to period in experiment
	gen quarter = quarter(entnahmedatum)
	replace quarter = quarter + 4 if year == 2012
	replace quarter = quarter + 8 if year == 2013

	gen week = week(entnahmedatum)
	
	lab var don "\$\widehat{Donation_{t-1}}\$"
	lab var adverse_weather "Adv. Weather\$ _{t-1}\$"
	lab var phonecall_plus1 "Call\$ _{t}\$"
	lab var phonecall "Call\$ _{t-1}\$"
	lab var adverse_weather_plus1 "Adv. Weather\$ _{t}\$"

	/* Regression */
	// Column 1:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy4 male age ///
	if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst
	
	gen e_sample_adverse = e(sample)
	estimates restore _ivreg2_don
	
	outreg2 using output/tables/tableD1, tex replace ///
	ctitle("1\$ ^{st}\$ stage", "Donation\$ _{t-1}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, \, Kleibergen/Paap F-statistic, \, ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	nor2 noobs nocons

	// Column 2:
	ivreghdfe don_plus1 (don = adverse_weather phonecall) adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy4 male age ///
	if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst
	
	local kp: di %4.2f `e(widstat)'
	local sh: di %4.3f `e(jp)'
	
	outreg2 using output/tables/tableD1, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Both, Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, `sh' , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	nor2 noobs nocons

	// Column 3:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather_plus1 phonecall_plus1 ///
	bltype_dummy2 bltype_dummy4 male age if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst
	
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using output/tables/tableD1, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Call, Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	nor2 noobs nocons

	// Column 4:
	ivreghdfe don_plus1 (don = adverse_weather) adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy4 male age ///
	if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst
	
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using output/tables/tableD1, ///
	tex append ///
	ctitle("2\$ ^{nd}\$ stages", "Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Adv. Weather, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	nor2 noobs nocons
	
	// Column 5:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy4 male age ///
	if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst
	
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using output/tables/tableD1, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Call, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	nor2 noobs nocons	
	
	// Column 6:
	ivreghdfe don_plus1 (don = adverse_weather) adverse_weather_plus1 phonecall ///
	phonecall_plus1 bltype_dummy2 bltype_dummy4 male age ///
	if no_don == 1 & bltype != 3 & bltype != 5, ///
	cluster(c_num id_cb) absorb(week gr_unique) first savefirst

	local kp: di %4.2f `e(widstat)'
	
	outreg2 using output/tables/tableD1, ///
	tex(frag) append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Adv. Weather, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy4) ///
	sortvar(don phonecall adverse_weather phonecall_plus1 adverse_weather_plus1) ///
	nor2 noobs nocons ///
	label
